package com.demo.wrapper;

import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.util.ObjectBuilder;
import com.demo.wrapper.enums.QueryEnum;
import com.demo.wrapper.interfaces.ICustomBuildQueryWrapper;
import com.demo.wrapper.pojo.Arg;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 支持lambda的查询拼接器
 *
 * @param <DO> 文档实体类
 * @author 19242
 */
public class EsLambdaQueryWrapper<DO> extends AbstractEsLambdaQueryWrapper<EsLambdaQueryWrapper<DO>, DO, EsLambdaAggWrapper<DO, EsLambdaQueryWrapper<DO>>, Query.Builder, SearchRequest.Builder>
        implements ICustomBuildQueryWrapper<EsLambdaQueryWrapper<DO>, EsLambdaAggWrapper<DO, EsLambdaQueryWrapper<DO>>, Function<Query.Builder, ObjectBuilder<Query>>, Function<SearchRequest.Builder, SearchRequest.Builder>> {

    public EsLambdaQueryWrapper() {
        super();
    }

    @Override
    public EsLambdaQueryWrapper<DO> instanceChild() {
        return new EsLambdaQueryWrapper<>();
    }

    @Override
    public EsLambdaQueryWrapper<DO> agg(Consumer<EsLambdaAggWrapper<DO, EsLambdaQueryWrapper<DO>>> consumer) {
        EsLambdaAggWrapper<DO, EsLambdaQueryWrapper<DO>> esAggWrapper = new EsLambdaAggWrapper<>();
        consumer.accept(esAggWrapper);
        return addLimit(new Arg(QueryEnum.AGG, esAggWrapper.getAgg().getName(), WrapperUtil.getAggregationBuilder(esAggWrapper)));
    }

    @Override
    public Query.Builder queryBuilder(boolean autoFilter) {
        return WrapperUtil.getBuilder(this, autoFilter);
    }

    @Override
    public SearchRequest.Builder sourceBuilder(boolean autoFilter) {
        return WrapperUtil.getSourceBuilder(this, autoFilter);
    }

}
